using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;


/// <summary>
/// Summary description for FilesWS
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class FilesWS : System.Web.Services.WebService
{
    public AuthHeader Authentication;
    private LoginWS loginWS;

    #region Constructor
    public FilesWS()
    {
        //Uncomment the following line if using designed components 
        InitializeComponent();
        loginWS = new LoginWS();
    } 
    #endregion

    #region Methods - Private
    private void InitializeComponent()
    {
    }
    #endregion

    #region Services
    [SoapHeader("Authentication")]
    [WebMethod(Description = "")]
    public bool DeleteFile(Guid guid, string folder, string file)
    {
        bool result = false;
        if (loginWS.AuthenticateUser(Authentication.Username, Authentication.Password))
        {
            result = SoinusDBM.Instance.DeleteFile(guid, folder, file, Authentication.Username);
        }
        return result;
    }

    [SoapHeader("Authentication")]
    [WebMethod(Description = "")]
    public bool DeleteFolder(Guid guid, string folder)
    {
        bool result = false;
        if (loginWS.AuthenticateUser(Authentication.Username, Authentication.Password))
        {
            result = SoinusDBM.Instance.DeleteFolder(guid, folder, Authentication.Username);
        }
        return result;
    }

    [SoapHeader("Authentication")]
    [WebMethod(Description = "")]
    public bool DeletePC(Guid guid)
    {
        bool result = false;
        if (loginWS.AuthenticateUser(Authentication.Username, Authentication.Password))
        {
            result = SoinusDBM.Instance.DeletePC(guid, Authentication.Username);
        }
        return result;
    }

    [SoapHeader("Authentication")]
    [WebMethod(Description = "")]
    public bool RegisterFile(Guid guid, string folder, string file)
    {
        bool result = false;
        if (loginWS.AuthenticateUser(Authentication.Username, Authentication.Password))
        {
            result = SoinusDBM.Instance.RegisterFile(guid, folder, file, Authentication.Username);
        }
        return result;
    }

    [SoapHeader("Authentication")]
    [WebMethod(Description = "")]
    public bool RegisterFolder(Guid guid, string folder)
    {
        bool result = false;
        if (loginWS.AuthenticateUser(Authentication.Username, Authentication.Password))
        {
            result = SoinusDBM.Instance.RegisterFolder(guid, folder, Authentication.Username);
        }
        return result;
    }

    [SoapHeader("Authentication")]
    [WebMethod(Description = "")]
    public bool RegisterPC(Guid guid)
    {        
        bool result = false;
        string ip = this.Context.Request.UserHostAddress;

        if (ip.Length < 7)
        {
            ip = "127.0.0.1";
        }

        if (loginWS.AuthenticateUser(Authentication.Username, Authentication.Password))
        {            
            result = SoinusDBM.Instance.RegisterPC(guid, Authentication.Username, ip);
        }
        return result;
    }

    [SoapHeader("Authentication")]
    [WebMethod(Description = "")]
    public StoredFileInfo[] GetUserFiles()
    {
        List<StoredFileInfo> result = null;
        if (loginWS.AuthenticateUser(Authentication.Username, Authentication.Password))
        {
            result = SoinusDBM.Instance.GetUserFiles(Authentication.Username);
        }
        return result.ToArray();
    } 
    #endregion

}

public class AuthHeader : SoapHeader
{
    public string Username;
    public string Password;
}

public class StoredFileInfo
{
    #region Fields
    private string _name;
    private string _folder;
    private string _ip;
    #endregion

    #region Properties
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
    public string Folder
    {
        get { return _folder; }
        set { _folder = value; }
    }
    public string Ip
    {
        get { return _ip; }
        set { _ip = value; }
    }
    #endregion

    #region Constructor
    public StoredFileInfo() { }
    public StoredFileInfo(string name, string folder, string ip)
    {
        this._name = name;
        this._folder = folder;
        this._ip = ip;
    }
    #endregion
}